package com.franklin.java.utils;

import com.franklin.java.api.psi.data.PsiCallingElement;
import com.franklin.java.completion.JavaDataCompletionContributorSupport;
import com.franklin.java.configuration.sql.completion.ColorState;
import com.franklin.java.icon.PluginIcons;
import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.completion.JavaMethodCallElement;
import com.intellij.codeInsight.completion.PrioritizedLookupElement;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.codeInsight.lookup.LookupElementDecorator;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;

import java.awt.*;
import java.util.Objects;

/**
 * @author Franklin
 * @since 2021/8/3 18:38
 */
public class CompletionUtils {

    public static LookupElement getPrioritizedLookupElement(LookupElement lookupElement,double priority) {
        return PrioritizedLookupElement.withPriority(lookupElement, priority);
    }

    /**
     * 移动光标
     * @param editor
     * @param moveLength
     */
    public static void moveMouse(Editor editor,int moveLength){
        CaretModel caretModel = editor.getCaretModel();
        caretModel.moveToOffset(caretModel.getOffset() + moveLength);
    }

    public static LookupElement getLookElement(
        String code,
        String typeText,
        String fillExpression,
        String variable,
        String tailText,
        int moveBefore
    ) {
        InsertHandler<LookupElement> lookupElementInsertHandler = (context, lookupElement) -> {
            PsiFile file = context.getFile();
            PsiCallingElement psiCallingElement = PsiUtils.findCallingElement(file, context.getStartOffset());
            if (Objects.nonNull(psiCallingElement)) {
                PsiElement callingElement = psiCallingElement.getCallingElement();
                TextRange callingRange = callingElement.getTextRange();

                context.getDocument().deleteString(callingRange.getStartOffset(), context.getTailOffset());

                String result = fillExpression.replace("$" + variable + "$", callingElement.getText());

                context.getDocument().replaceString(callingRange.getStartOffset(), callingRange.getStartOffset(), result);
                context.commitDocument();
                CaretModel caretModel = context.getEditor().getCaretModel();
                caretModel.moveToOffset(caretModel.getOffset() + result.length() - moveBefore);
            }
        };
        LookupElementBuilder lookupElementBuilder = LookupElementBuilder.create(code)
            .withItemTextForeground(ColorState.getColor(ProjectUtils.getCurrProject()))
            .withInsertHandler(
                    lookupElementInsertHandler)
            .withIcon(PluginIcons.SERVICE).withTailText(tailText).withTypeText(typeText);
        return lookupElementBuilder;
    }

    public static LookupElement getLookElement(
            String code,
            String typeText,
            String fillExpression,
            String variable,
            String tailText,
            int moveBefore,
            InsertHandler<LookupElement> insertHandler
    ) {
        LookupElementBuilder lookupElementBuilder = LookupElementBuilder.create(code)
                .withItemTextForeground(ColorState.getColor(ProjectUtils.getCurrProject()))
                .withInsertHandler(insertHandler)
                .withIcon(PluginIcons.SERVICE).withTailText(tailText).withTypeText(typeText);
        return lookupElementBuilder;
    }

    public static LookupElement getLookElement(
            String code,
            String typeText,
            String tailText,
            InsertHandler<LookupElement> insertHandler
    ) {
        LookupElementBuilder lookupElementBuilder = LookupElementBuilder.create(code)
                .withItemTextForeground(ColorState.getColor(ProjectUtils.getCurrProject()))
                .withInsertHandler(insertHandler)
                .withIcon(PluginIcons.SERVICE).withTailText(tailText).withTypeText(typeText);
        return lookupElementBuilder;
    }
}
